﻿using System;
using System.Linq;
using System.Web;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using LJSheng.Data;
using EntityFramework.Extensions;
using LJSheng.Common;

namespace LJSheng.Web.ajax
{
    /// <summary>
    /// api 的摘要说明
    /// </summary>
    public class api : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string returnstr = "当前设备被禁止访问";
            string ff = string.IsNullOrEmpty(context.Request.Form["ff"]) ? context.Request.QueryString["ff"] : context.Request.Form["ff"];
            switch (ff)
            {
                case "yzm":
                    returnstr = Getyzm(context.Request.Form["shouji"], Int32.Parse(context.Request.Form["lx"]));
                    break;
                case "tjr":
                    returnstr = tjr(context.Request.Form["tjr"], context.Request.Form["lx"]);
                    break;
                case "gopay":
                    returnstr = gopay(context.Request.Form["shopgid"], context.Request.Form["gmsl"]);
                    break;
                case "pay":
                    returnstr = pay(context.Request.Form["shopgid"], context.Request.Form["yj"]);
                    break;
                case "wxpay":
                    returnstr = wxpay(context.Request.Form["gid"]);
                    break;
                case "userlist":
                    returnstr = userlist(context.Request.Form["gid"]);
                    break;
                case "pj":
                    returnstr = pj(context.Request.Form["plgid"], context.Request.Form["cpgid"], context.Request.Form["ddgid"], context.Request.Form["lx"], context.Request.Form["msg"], context.Request.Form["pf"]);
                    break;
                case "shoppwd":
                    returnstr = shoppwd(context);
                    break;
                case "oauth2":
                    oauth2(context);
                    break;
                case "ewm":
                    returnstr = EWM(context);
                    break;
                case "shopewm":
                    returnstr = ShopEWM(context);
                    break;
                case "shoppicture":
                    returnstr = ShopPicture(context);
                    break;
                case "dmzfile":
                    returnstr = DMZFile(context);
                    break;
                case "grzl":
                    returnstr = grzl(context);
                    break;
                case "sfz":
                    returnstr = sfz(context);
                    break;
                case "chaimg":
                    returnstr = chaimg(context);
                    break;
                case "shoppay":
                    returnstr = shoppay(context);
                    break;
                case "fs":
                    returnstr = fs(context);
                    break;
                case "setrmb":
                    returnstr = setrmb(context);
                    break;
                case "getjiqi":
                    returnstr = getjiqi(context);
                    break;
                default:
                    break;
            }
            context.Response.Write(returnstr);
            context.Response.End();
        }
        #region 订单其他
        /// <summary>
        /// 商家提交订单给会员
        /// </summary>
        /// <param name="shopgid">商家gid</param>
        /// <param name="yj">消费金额</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string shoppay(HttpContext context)
        {
            string ck = Common.LCookie.GetCookie("shop");
            if (string.IsNullOrEmpty(ck))
            {
                return JsonConvert.SerializeObject(new AjaxResult(300, "请先登录"));
            }
            else
            {
                Guid buygid = Common.LCookie.GetShop(2);
                Guid usergid = Guid.Parse(context.Request["usergid"]);
                decimal amount = decimal.Parse(context.Request["amount"]);
                decimal rmb = decimal.Parse(context.Request["rmb"]);
                decimal ptf = decimal.Parse(context.Request["ptf"]);
                using (EFDB db = new EFDB())
                {
                    string order_no = RandStr.CreateOrderNO();
                    //生成订单
                    var b = new LOrder();
                    b.gid = Guid.NewGuid();
                    b.addtime = DateTime.Now;
                    b.usergid = usergid;
                    b.buygid = buygid;
                    b.order_no = order_no;
                    b.pay_status = 2;
                    b.pay_type = 2;
                    b.amount = amount;
                    b.rmb = rmb;
                    b.pay_amount = 0;
                    b.user_cash = rmb - ptf;
                    b.price = rmb;
                    b.num = 1;
                    b.coupon_price = 0;
                    b.coupon_number = ptf.ToString();
                    b.ptf = ptf * 0.3M;
                    b.hyfwf = ptf * 0.4M;
                    b.tgf = ptf * 0.3M;
                    b.zhekou = context.Request["zhekou"];
                    db.LOrder.Add(b);
                    if (db.SaveChanges() == 1)
                    {
                        return JsonConvert.SerializeObject(new AjaxResult("成功"));
                    }
                    else
                    {
                        return JsonConvert.SerializeObject(new AjaxResult(300, "提交订单失败"));
                    }
                }
            }
        }

        /// <summary>
        /// 注册会员订单
        /// </summary>
        /// <param name="shopgid">商家gid</param>
        /// <param name="lx">1=会员注册 2=商家注册</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string gopay(string shopgid, string gmsl)
        {
            int num = int.Parse(gmsl);
            if (num > 100)
            {
                return "{\"appId\":\"payerr\",\"return_msg\":\"一个订单最多购买100个卡密\"}";
            }
            else
            {
                Guid usergid = LCookie.GetUser();
                Guid buygid = Guid.Parse(shopgid);
                using (EFDB db = new EFDB())
                {
                    var s = db.ShopList.Where(l => l.gid == buygid).FirstOrDefault();
                    var t = db.TC.Where(l => l.shopgid == buygid && l.usergid == usergid).FirstOrDefault();
                    var u = db.LUser.Where(l => l.gid == usergid).FirstOrDefault();
                    if (u != null && t != null && s != null)
                    {
                        string kami;
                        System.Text.StringBuilder sb = new System.Text.StringBuilder();
                        decimal allrmb = t.rmb * num;//实付金额
                        string orderno = RandStr.CreateOrderNO();
                        if (allrmb <= u.rmb)
                        {
                            for (int i = 0; i < num; i++)
                            {
                                kami = Guid.NewGuid().ToString().Replace("-", "");
                                //生成订单
                                var b = new LOrder();
                                b.gid = Guid.NewGuid();
                                b.addtime = DateTime.Now;
                                b.usergid = usergid;
                                b.buygid = buygid;
                                b.order_no = orderno;
                                b.pay_status = 2;
                                b.pay_type = 2;
                                b.amount = t.rmb * num;//原价
                                b.rmb = allrmb;//实付
                                b.pay_amount = 0;
                                b.user_cash = t.rmb;//单价
                                b.price = t.rmb;
                                b.num = num;
                                b.coupon_price = 0;
                                b.coupon_number = kami;
                                b.ptf = u.zk;//购买时候折扣
                                b.hyfwf = 0;
                                b.tgf = 0;
                                b.tianshu = s.xfx;
                                b.hyfwf = s.xfd;
                                db.LOrder.Add(b);
                                sb.Append(kami + "#");
                            }
                            int onum = db.SaveChanges();
                            if (onum > 0)
                            {
                                if (db.LUser.Where(l => l.gid == usergid).Update(l => new LUser { rmb = l.rmb - allrmb }) == 1)
                                {
                                    return "{\"appId\":\"payok\",\"return_msg\":\"" + sb.ToString() + "\"}";
                                }
                                else
                                {
                                    Common.LogManager.WriteLog("支付失败", "订单号=" + orderno + ",用户gid=" + shopgid + ",套餐gid=" + buygid.ToString() + ",支付金额=" + allrmb.ToString() + ",购买数量=" + gmsl.ToString() + ",生成卡密数量=" + onum.ToString());
                                    return "{\"appId\":\"payerr\",\"return_msg\":\"扣除失败\"}";
                                }
                            }
                            else
                            {
                                return "{\"appId\":\"payerr\",\"return_msg\":\"提交订单失败\"}";
                            }
                        }
                        else
                        {
                            return "{\"appId\":\"payerr\",\"return_msg\":\"余额不足\"}";
                        }
                    }
                    else
                    {
                        return "{\"appId\":\"payerr\",\"return_msg\":\"请重新登录购买\"}";
                    }
                }
            }
        }

        /// <summary>
        /// 用户消费订单
        /// </summary>
        /// <param name="shopgid">商家gid</param>
        /// <param name="yj">消费金额</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string pay(string shopgid, string yj)
        {
            string ck = Common.LCookie.GetCookie("linjiansheng");
            if (string.IsNullOrEmpty(ck))
            {
                return "{\"appId\":\"payerr\",\"return_msg\":\"请先登录\"}";
            }
            else
            {
                JObject json = JsonConvert.DeserializeObject(Common.DESRSA.DESDeljsheng(ck)) as JObject;
                Guid gid = Guid.Parse(json["gid"].ToString());
                using (EFDB db = new EFDB())
                {
                    var u = db.LUser.Where(l => l.gid == gid).FirstOrDefault();
                    if (u.loginid == json["loginid"].ToString())
                    {
                        if (string.IsNullOrEmpty(json["grade"].ToString()))
                        {
                            return "{\"appId\":\"payerr\",\"return_msg\":\"请先升级会员\"}";
                        }
                        else
                        {
                            decimal rmb = decimal.Parse(yj);
                            Guid buygid = Guid.Parse(shopgid);
                            var b = db.ShopList.Where(l => l.gid == buygid).FirstOrDefault();
                            if (b != null)
                            {
                                decimal zhekou = 0;
                                decimal fl = 0;
                                return Helper.wxpay(buygid, b.name, 0, rmb, zhekou, fl, rmb, 1, 0.3M, 0.4M, 0.3M);
                            }
                            else
                            {
                                return "{\"appId\":\"payerr\",\"return_msg\":\"购买商品不存在\"}";
                            }
                        }
                    }
                    else
                    {
                        return "{\"appId\":\"payerr\",\"return_msg\":\"请先登录\"}";
                    }
                }
            }
        }

        /// <summary>
        /// 开始微信支付
        /// </summary>
        /// <param name="gid">订单gid</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string wxpay(string gid)
        {
            string ck = Common.LCookie.GetCookie("linjiansheng");
            if (string.IsNullOrEmpty(ck))
            {
                return "{\"appId\":\"payerr\",\"return_msg\":\"请先登录\"}";
            }
            else
            {
                Guid ogid = Guid.Parse(gid);
                using (EFDB db = new EFDB())
                {
                    var b = db.LOrder.Where(l => l.gid == ogid).FirstOrDefault();
                    if (b != null)
                    {
                        string beizhu = JsonConvert.SerializeObject(new { lx = 0, b.rmb, b.buygid, b.usergid });//备注
                        //开始微信统一下单
                        string _Pay_Package = "";
                        //微信jspai支付
                        if (_Pay_Package.Length > 0)
                        {
                            return _Pay_Package;
                        }
                    }
                    else
                    {
                        return "{\"appId\":\"payerr\",\"return_msg\":\"订单不存在!\"}";
                    }
                }
                return "{\"appId\":\"payerr\",\"return_msg\":\"调用微信支付异常\"}";
            }
        }

        /// <summary>
        /// 推荐人列表
        /// </summary>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public static string userlist(string gid)
        {
            return Helper.userlist(gid);
        }

        /// <summary> 
        /// 微信网页授权页面调用
        /// </summary> 
        /// <param name="逻辑说明"></param> 
        /// <param>修改备注</param> 
        /// 2014-5-20 林建生
        public void oauth2(HttpContext context)
        {
            string tourl = context.Request.QueryString["tourl"];
            string openid = Common.LCookie.GetCookie("openid");
            if (String.IsNullOrEmpty(openid))
            {
                var code = context.Request.QueryString["code"];
                if (string.IsNullOrEmpty(code))
                {
                    var url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri=" + context.Server.UrlEncode(System.Web.Configuration.WebConfigurationManager.AppSettings["url"].ToString() + "/ajax/api.ashx?ff=oauth2&tourl=" + tourl) + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect", System.Web.Configuration.WebConfigurationManager.AppSettings["AppId"].ToString());
                    context.Response.Redirect(url);
                }
                else
                {
                    var client = new System.Net.WebClient();
                    client.Encoding = System.Text.Encoding.UTF8;
                    var url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", System.Web.Configuration.WebConfigurationManager.AppSettings["AppId"].ToString(), System.Web.Configuration.WebConfigurationManager.AppSettings["AppSecret"].ToString(), code);
                    JObject jb = JObject.Parse(client.DownloadString(url));
                    openid = jb["openid"].ToString();
                    Common.LCookie.AddCookie("openid", openid, 30);
                }
            }
            //跳转
            string gourl = tourl.Replace("$", "?").Replace("@", "&");
            if (String.IsNullOrEmpty(openid))
            {
                gourl = "/home/index";
            }
            //Common.LogManager.WriteLog("获取openid","openid=" + openid + ",URL=" + gourl + ",tourl=" + tourl);
            context.Response.Redirect(gourl);
        }
        #endregion

        #region 评价
        /// <summary>
        ///  会员评价
        /// </summary>
        /// <param name="plgid">评价的gid</param>
        /// <param name="cp">商家gid</param>
        /// <param name="ddgid"> </param>
        /// <param name="lx">评价类型[0=商家请求]</param>
        /// <param name="msg">评价内容</param>
        /// <param name="pf">评分</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string pj(string plgid, string cp, string dd, string lx, string msg, string pf)
        {
            using (EFDB db = new EFDB())
            {
                if (plgid != "0" && lx == "0")
                {
                    //商家回复
                    Guid gid = Guid.Parse(plgid);
                    PL b = db.PL.Where(l => l.gid == gid).FirstOrDefault();
                    b.tomsg = msg;
                    b.userpj = Int32.Parse(pf);
                    b.pltime = DateTime.Now;
                    if (db.SaveChanges() == 1)
                    {
                        return JsonConvert.SerializeObject(new AjaxResult(new { msg = "回复成功", plgid }));
                    }
                    else
                    {
                        return JsonConvert.SerializeObject(new AjaxResult(300, new { msg = "回复失败,请返回重新进来" }));
                    }
                }
                else
                {
                    Guid usergid = Common.LCookie.GetUser();
                    Guid cpgid = Guid.Parse(cp);
                    Guid ddgid = Guid.Parse(dd);
                    PL b = db.PL.Where(l => l.ddgid == ddgid && l.usergid == usergid).FirstOrDefault();
                    if (b == null)
                    {
                        b = new PL();
                        b.gid = Guid.NewGuid();
                        b.addtime = DateTime.Now;
                        b.show = 1;
                        b.sort = 1;
                        b.msg = msg;
                        b.ddgid = ddgid;
                        b.cpgid = cpgid;
                        b.usergid = usergid;
                        b.shoppj = Int32.Parse(pf);
                        db.PL.Add(b);
                    }
                    else
                    {
                        b.msg = msg;
                        b.tomsg = "等待回复";
                        b.shoppj = Int32.Parse(pf);
                    }
                    if (db.SaveChanges() == 1)
                    {
                        if (pf == "1")
                        {
                            db.ShopList.Where(l => l.gid == cpgid).Update(l => new ShopList { sort = l.sort + 1 });
                        }
                        else if (pf == "3")
                        {
                            db.ShopList.Where(l => l.gid == cpgid).Update(l => new ShopList { sort = l.sort - 1 });
                        }
                        return JsonConvert.SerializeObject(new AjaxResult(new { msg = "评价成功", plgid = b.gid }));
                    }
                    else
                    {
                        return JsonConvert.SerializeObject(new AjaxResult(300, new { msg = "评价失败,请返回重新进来" }));
                    }
                }
            }
        }

        /// <summary>
        /// 评价图片
        /// </summary>
        /// <param name="context">传入参数</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string chaimg(HttpContext context)
        {
            using (EFDB db = new EFDB())
            {
                Guid plgid = Guid.Parse(context.Request["data[plgid]"]);
                int lx = Int32.Parse(context.Request["data[lx]"]);
                int num = db.pllist.Where(l => l.plgid == plgid && l.lx == lx).Count();
                if (num <= 5)
                {
                    string picture = Helper.jsimg(LJShengHelper.PJ, context.Request.Form["base64Data"]);
                    var b = new pllist();
                    b.gid = Guid.NewGuid();
                    b.addtime = DateTime.Now;
                    b.lx = lx;
                    b.plgid = plgid;
                    b.picture = picture;
                    db.pllist.Add(b);
                    if (db.SaveChanges() == 1)
                    {
                        return JsonConvert.SerializeObject(new AjaxResult(new { file = LJShengHelper.PJ + picture, msg = "上传成功,最多5张,已上传:" + (num + 1).ToString() }));
                    }
                    else
                    {
                        return JsonConvert.SerializeObject(new AjaxResult(300, new { msg = "操作失败" }));
                    }
                }
                else
                {
                    return JsonConvert.SerializeObject(new AjaxResult(300, new { msg = "上传失败,最多只能上传5张" }));
                }
            }
        }
        #endregion

        #region 获取验证码
        /// <summary> 
        /// 获取验证码
        /// </summary> 
        /// <param name="shouji">发送的手机号</param> 
        /// <param name="lx">发送类型[1=注册 2=纯验证码]</param> 
        /// <param name="return">success:本次发送短信编号,如果发送失败，则返回：error:错误描述</param> 
        /// <param name="逻辑说明"></param> 
        /// <param>修改备注</param> 
        /// 2014-5-20 林建生
        /// 
        public string Getyzm(string shouji, int lx)
        {
            if (shouji.Length == 11)
            {
                using (EFDB db = new EFDB())
                {
                    //获取短信的时候先判断是否已经被注册
                    var u = db.LUser.Where(l => l.account == shouji).FirstOrDefault();
                    if ((u == null && lx == 1) || (u != null && lx == 2))
                    {
                        string yzm = Common.RandStr.CreateValidateNumber(4);
                        var btime = db.SMS.Where(l => l.shouji == shouji && l.lx == 1).OrderByDescending(l => l.addtime).FirstOrDefault();
                        if (btime != null)
                        {
                            //判断是否十分钟内重发
                            TimeSpan ts = DateTime.Now - DateTime.Parse(btime.addtime.ToString());
                            if (ts.TotalMinutes <= 10)
                            {
                                yzm = btime.msg;
                                //isyzm = false;//重发,不插入短信内容
                            }
                        }

                        return "";
                    }
                    else
                    {
                        if (lx == 2)
                        {
                            return "{\"resp\":{\"respCode\":\"111111\",\"templateSMS\":\"手机号未注册\"}}";
                        }
                        else
                        {
                            return "{\"resp\":{\"respCode\":\"111111\",\"templateSMS\":\"手机号已注册\"}}";
                        }
                    }
                }
            }
            else
            {
                return "{\"resp\":{\"respCode\":\"111111\",\"templateSMS\":\"手机号错误\"}}";
            }
        }

        #endregion

        #region 推荐人查询
        /// <summary>
        /// 推荐人查询
        /// </summary>
        public string tjr(string tjr, string lx)
        {
            return Helper.tjr(tjr, lx);
        }
        #endregion

        #region 会员
        /// <summary>
        /// 用户二维码
        /// </summary>
        /// <param name="context">传入参数</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string EWM(HttpContext context)
        {
            using (EFDB db = new EFDB())
            {
                Guid gid = Common.LCookie.GetUser();
                string picture = Helper.jsimg(LJShengHelper.EWM, context.Request.Form["base64Data"]);
                if (db.LUser.Where(l => l.gid == gid).Update(l => new LUser { ewm = picture }) == 1)
                {
                    return JsonConvert.SerializeObject(new AjaxResult(new { file = LJShengHelper.EWM + picture, msg = "操作成功" }));
                }
                else
                {
                    return JsonConvert.SerializeObject(new AjaxResult(300, new { msg = "操作失败" }));
                }
            }
        }

        /// <summary>
        /// 个人资料
        /// </summary>
        /// <param name="context">传入参数</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string grzl(HttpContext context)
        {
            Guid gid = Common.LCookie.GetUser();
            using (EFDB db = new EFDB())
            {
                string Province = context.Request["data[Province]"];
                string City = context.Request["data[City]"];
                string Area = context.Request["data[Area]"];
                string nickname = context.Request["data[nickname]"];
                string pwd = context.Request["data[pwd]"];
                string newpwd = context.Request["data[newpwd]"];
                var b = db.LUser.Where(l => l.gid == gid).FirstOrDefault();
                if (!string.IsNullOrEmpty(pwd) && Common.MD5.GetMD5ljsheng(pwd) == b.pwd)
                {
                    b.pwd = Common.MD5.GetMD5ljsheng(newpwd);
                }
                HttpFileCollection files = context.Request.Files;
                string picture = Helper.jsimg(LJShengHelper.User, context.Request.Form["base64Data"]);
                if (!string.IsNullOrEmpty(picture))
                {
                    b.picture = picture;
                }
                b.province = Province;
                b.city = City;
                b.area = Area;
                b.nickname = nickname;
                if (db.SaveChanges() == 1)
                {
                    return JsonConvert.SerializeObject(new AjaxResult(new { file = LJShengHelper.User + picture, msg = "操作成功" }));
                }
                else
                {
                    return JsonConvert.SerializeObject(new AjaxResult(300, new { msg = "操作失败" }));
                }
            }
        }

        /// <summary>
        /// 身份证
        /// </summary>
        /// <param name="context">传入参数</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string sfz(HttpContext context)
        {
            Guid gid = Common.LCookie.GetUser();
            using (EFDB db = new EFDB())
            {
                string real_name = context.Request["data[real_name]"];
                string sfz = context.Request["data[haoma]"];
                string khh = context.Request["data[khh]"];
                string khm = context.Request["data[khm]"];
                string kahao = context.Request["data[kahao]"];
                string sex = context.Request["data[sex]"];
                var b = db.LUser.Where(l => l.gid == gid).FirstOrDefault();
                string picture = Helper.jsimg(LJShengHelper.SFZ, context.Request.Form["base64Data"]);
                if (!string.IsNullOrEmpty(picture))
                {
                    b.sfzpicture = picture;
                }
                b.real_name = real_name;
                b.sfz = sfz;
                b.khh = khh;
                b.khm = khm;
                b.kahao = kahao;
                b.sex = Int32.Parse(sex);
                if (db.SaveChanges() == 1)
                {
                    return JsonConvert.SerializeObject(new AjaxResult(new { file = LJShengHelper.SFZ + picture, msg = "操作成功" }));
                }
                else
                {
                    return JsonConvert.SerializeObject(new AjaxResult(300, new { msg = "操作失败" }));
                }
            }
        }
        #endregion

        #region 商家
        /// <summary>
        /// 商家二维码
        /// </summary>
        /// <param name="context">传入参数</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string ShopEWM(HttpContext context)
        {
            Guid gid = Common.LCookie.GetShop(1);
            Guid shopgid = Common.LCookie.GetShop(2);
            using (EFDB db = new EFDB())
            {
                string picture = Helper.jsimg(LJShengHelper.EWM, context.Request.Form["base64Data"]);
                if (db.LUser.Where(l => l.gid == gid).Update(l => new LUser { ewm = picture }) == 1)
                {
                    db.ShopList.Where(l => l.gid == shopgid).Update(l => new ShopList { ewm = picture });
                    return JsonConvert.SerializeObject(new AjaxResult(new { file = LJShengHelper.EWM + picture, msg = "操作成功" }));
                }
                else
                {
                    return JsonConvert.SerializeObject(new AjaxResult(300, new { msg = "操作失败" }));
                }
            }
        }

        /// <summary>
        /// 商家代码证
        /// </summary>
        /// <param name="context">传入参数</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string ShopPicture(HttpContext context)
        {
            Guid shopgid = Common.LCookie.GetShop(2);
            using (EFDB db = new EFDB())
            {
                string picture = Helper.jsimg(LJShengHelper.Shop, context.Request.Form["base64Data"]);
                if (db.ShopList.Where(l => l.gid == shopgid).Update(l => new ShopList { picture = picture }) == 1)
                {
                    return JsonConvert.SerializeObject(new AjaxResult(new { file = LJShengHelper.Shop + picture, msg = "操作成功" }));
                }
                else
                {
                    return JsonConvert.SerializeObject(new AjaxResult(300, new { msg = "操作失败" }));
                }
            }
        }

        /// <summary>
        /// 商家代码证
        /// </summary>
        /// <param name="context">传入参数</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string DMZFile(HttpContext context)
        {
            Guid shopgid = Common.LCookie.GetShop(2);
            using (EFDB db = new EFDB())
            {
                string khh = context.Request["data[khh]"];
                string khm = context.Request["data[khm]"];
                string kahao = context.Request["data[kahao]"];
                var b = db.ShopList.Where(l => l.gid == shopgid).FirstOrDefault();
                string picture = Helper.jsimg(LJShengHelper.DMZ, context.Request.Form["base64Data"]);
                if (!string.IsNullOrEmpty(picture))
                {
                    b.dmz = picture;
                }
                b.khh = khh;
                b.khm = khm;
                b.kahao = kahao;
                if (db.SaveChanges() == 1)
                {
                    db.LUser.Where(l => l.gid == b.usergid).Update(l => new LUser { khh = khh, khm = khm, kahao = kahao });
                    return JsonConvert.SerializeObject(new AjaxResult(new { file = LJShengHelper.DMZ + picture, msg = "操作成功" }));
                }
                else
                {
                    return JsonConvert.SerializeObject(new AjaxResult(300, new { msg = "操作失败" }));
                }
            }
        }

        /// <summary>
        /// 更新商家密码-个人
        /// </summary>
        /// <param name="context">传入参数</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string shoppwd(HttpContext context)
        {
            Guid gid = Common.LCookie.GetShop(1);
            using (EFDB db = new EFDB())
            {
                string pwd = context.Request["pwd"];
                string newpwd = context.Request["newpwd"];
                var b = db.LUser.Where(l => l.gid == gid).FirstOrDefault();
                if (newpwd.Length >= 6 && !string.IsNullOrEmpty(pwd) && Common.MD5.GetMD5ljsheng(pwd) == b.pwd)
                {
                    b.pwd = Common.MD5.GetMD5ljsheng(newpwd);
                }
                else
                {
                    return JsonConvert.SerializeObject(new AjaxResult(300, "原密码错误!"));
                }
                if (db.SaveChanges() == 1)
                {
                    Helper.UPCKUser(gid);
                    return JsonConvert.SerializeObject(new AjaxResult(b.gid));
                }
                else
                {
                    return JsonConvert.SerializeObject(new AjaxResult(300, "更新失败!"));
                }
            }
        }
        #endregion

        /// <summary>
        /// 粉丝数
        /// </summary>
        /// <param name="context">传入参数</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public string fs(HttpContext context)
        {
            Guid gid = string.IsNullOrEmpty(context.Request.Form["gid"]) ? LCookie.GetUser() : Guid.Parse(context.Request.Form["gid"]);
            using (EFDB db = new EFDB())
            {
                int n = Helper.fs(db.LUser.Select(l => new User { gid = l.gid, usergid = l.usergid }).ToList(), gid, 3);
                db.LUser.Where(l => l.gid == gid).Update(l => new LUser { number = n });
                return JsonConvert.SerializeObject(new AjaxResult(n));
            }
        }

        /// <summary>
        /// 设置用户套餐价格
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string setrmb(HttpContext context)
        {
            decimal rmb = decimal.Parse(context.Request.Form["rmb"]);
            Guid usergid = Guid.Parse(context.Request.Form["usergid"]);
            Guid shopgid = Guid.Parse(context.Request.Form["shopgid"]);
            //Guid tcgid = string.IsNullOrEmpty(context.Request.Form["tcgid"])?Guid.NewGuid(): Guid.Parse(context.Request.Form["tcgid"]);
            using (EFDB db = new EFDB())
            {
                var b = db.TC.Where(l => l.shopgid == shopgid && l.usergid == usergid).FirstOrDefault();
                if (b == null)
                {
                    b = new Data.TC();
                    b.gid = Guid.NewGuid();
                    b.addtime = DateTime.Now;
                    b.shopgid = shopgid;
                    b.usergid = usergid;
                    b.rmb = rmb;
                    db.TC.Add(b);
                }
                else
                {
                    b.rmb = rmb;
                }
                if (db.SaveChanges() == 1)
                {
                    return JsonConvert.SerializeObject(new AjaxResult("修改成功"));
                }
                else
                {
                    return JsonConvert.SerializeObject(new AjaxResult("修改失败"));
                }
                //db.TC.Where(l => l.shopgid == shopgid && l.usergid == usergid).Update(l => new LUser { number = 1 });

            }

        }

        public string getjiqi(HttpContext context)
        {
            string token = context.Request.QueryString["token"];
            using (EFDB db = new EFDB())
            {
                var t = db.LJSheng.Where(l => l.account == "token" && l.explain != "").FirstOrDefault();
                string tk = "9021616";
                if (t != null)
                {
                    tk = t.explain;
                }
                if (token == tk)
                {
                    var b = db.LOrder.Where(l => l.pay_status == 1 && l.remarks != "" && l.pay_type != 4).OrderBy(l => l.pay_time).ToList().Select(s => new LOrder { remarks = s.remarks });
                    if (b != null)
                    {
                        System.Text.StringBuilder sb = new System.Text.StringBuilder();
                        foreach (var s in b)
                        {
                            try
                            {
                                DateTime dqsj = DateTime.Parse(s.remarks.Split(' ')[3].Replace("|", "").Replace(".", "-"));
                                if (dqsj >= DateTime.Now)
                                {
                                    sb.Append(s.remarks + "\r\n");
                                }
                            }
                            catch { sb.Append(s.remarks + "\r\n"); }
                        }
                        return sb.ToString();
                    }
                    else
                    {
                        return JsonConvert.SerializeObject(new AjaxResult("没有数据"));
                    }
                }
                else
                {
                    return JsonConvert.SerializeObject(new AjaxResult("非法请求"));
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}